<?php

namespace app\admin\controller;

use app\Request;
use app\admin\service\Product as ProductService;

class Product
{
    public function index($page=1,$size=10,$where='')
    {
        $list=(new ProductService())->getPageData($page,$size,$where);
        return success($list);
    }
    public function save(Request $request)
    {
        $data = $request->params;
        //return success($data);
        $res = (new ProductService())->edit($data);
        //return success($res);
        if (!$res) {
            return fail($res);
        } else {
            return success($data);
        }
    }
    public function update(Request $request)
    {
        $pros=new ProductService();
        $data = $request->params;
        $exist = !$pros->hasEmpty($data['id']);
        if ($exist) {
            $res = $pros->edit($data);
            if ($res) {
                return success($data);
            } else {
                return fail();
            }
        } else {
            return fail($data);
        }
    }
    public function delete(Request $request)
    {
        $id = $request->params["id"];
        $res = (new ProductService())->deleteByID($id);
        if ($res) {
            return success();
        } else {
            return fail();
        }
    }
    public function read(Request $request)
    {
        $id=$request->params['id'];
        $res=(new ProductService())->readByID($id);
        if ($res) {
            return success($res);
        } else {
            return fail();
        }
    }
}
